package jifenbao.com.jifenbao.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import jifenbao.com.jifenbao.R;
import jifenbao.com.jifenbao.UserInfoActivity;

/**
 * Created by longteng on 2016-5-24.
 */
public class DialogForRedeem extends Dialog{
    private TextView dialogcost,wrongcode,confirmBotton,cancelButton;
    private Context context;
    private EditText typeCode;

    public DialogForRedeem(Context context){
        super(context,android.R.style.Theme_Dialog);
        this.context = context;
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_redeem_dialog);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        dialogcost = (TextView)findViewById(R.id.redeem_dialog_cost);
        wrongcode = (TextView)findViewById(R.id.wrongCode);
        confirmBotton = (TextView)findViewById(R.id.btn_confirm);
        cancelButton = (TextView)findViewById(R.id.btn_cancel);
        typeCode = (EditText)findViewById(R.id.verifyCode);


        confirmBotton.setOnClickListener(ClickListen);
        cancelButton.setOnClickListener(ClickListen);
    }
    private boolean isValidTypeCode(String code){
        int value = Integer.parseInt(code);
        return value % 10 + value / 10 % 10 + value / 100 % 10 + value /1000 % 10 == 20;
    }
    private View.OnClickListener ClickListen = new View.OnClickListener(){
        public void onClick(View v){
            switch(v.getId()){
                case R.id.btn_confirm:
                    String code = typeCode.getText().toString();
                    if(TextUtils.isEmpty(code)){
                        typeCode.requestFocus();
                        wrongcode.setVisibility(View.GONE);
                    }else if(isValidTypeCode(code)){
                        wrongcode.setVisibility(View.GONE);
                        context.startActivity(new Intent(context, UserInfoActivity.class));
                        if(haha != null) haha.onRedeemSucceed();
                        dismiss();
                    }else{
                        wrongcode.setVisibility(View.VISIBLE);
                        typeCode.requestFocus();
                    }
                    break;
                case R.id.btn_cancel:
                    Toast.makeText(context,"取消兑换",Toast.LENGTH_SHORT).show();
                    dismiss();
                    break;
            }
        }
    };
    private RedeemInterface haha;
    public void setRedeemSucceed(RedeemInterface hehe) {
        this.haha = hehe;
    }

}
